package org.csu.forum.controller;


import org.csu.forum.common.CommonResponse;
import org.csu.forum.domain.PO.Account;
import org.csu.forum.domain.VO.Prior;
import org.csu.forum.service.AccountManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.csu.forum.utils.TokenUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class AccountManageController {
    @Autowired
    AccountManageService accountManageService;

    @Autowired
    HttpServletRequest httpServletRequest;

//    @InitBinder
//    public void initBinder(WebDataBinder binder) {
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        dateFormat.setLenient(false);
//        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
//    }


    @ResponseBody
    @GetMapping("/users")
    public CommonResponse<List<Account>> getUserInfoList(){
        //如果是管理员则可以获取所有用户的列表
        if(isAdmin())
            return CommonResponse.createForSuccess(accountManageService.getAccountList(),"查看用户列表成功");
        return CommonResponse.createForError("非管理员无法获取用户列表");
    }

    @ResponseBody
    @DeleteMapping("/users/{userid}")
    public CommonResponse<String> deleteUserByUserId(@PathVariable int userid){
        //如果是管理员则可以删除用户
        if(isAdmin()){
            accountManageService.deleteAccount(userid);
            return CommonResponse.createForSuccess("删除用户成功");
        }
        return CommonResponse.createForError("删除用户失败，非管理员无法删除用户");
    }

    @ResponseBody
    @GetMapping("/users/{userid}/priors")
    public CommonResponse<List<Prior>> getUserPriorList(@PathVariable int userid){
        //如果是管理员则可以获取用户权限
        if(isAdmin()){
            return CommonResponse.createForSuccess(accountManageService.getPriorList(userid),"查看用户权限成功");
        }else if(TokenUtils.getClaimByToken(httpServletRequest.getHeader("token"),"userid") == userid){
            return CommonResponse.createForSuccess(accountManageService.getPriorList(userid),"查看自己的权限成功");
        }
        return CommonResponse.createForError("查看用户权限失败，非管理员无法查看其他用户权限");
    }

    @ResponseBody
    @PutMapping("/users/{userid}/priors")
    public CommonResponse<Prior> setUserPrior(@RequestBody Prior prior) throws ParseException {
        //如果是管理员则可以设置用户权限
        if(isAdmin()){
            accountManageService.setPrior(prior);
            return CommonResponse.createForSuccess(prior,"设置用户权限成功");
        }
        return CommonResponse.createForError("设置用户权限失败");
    }

    @ResponseBody
    @PostMapping("/sections/{sectionid}/users/{userid}")
    public CommonResponse<String> addUserToSection(@RequestBody Prior prior){
        try {
            accountManageService.addAccountToSection(prior.getUserid(),prior.getSectionid());
            accountManageService.setPrior(prior);
            return CommonResponse.createForSuccess("添加用户到某个版块成功");
        }catch (Exception e){
            e.printStackTrace();
            return CommonResponse.createForError("添加用户到某个版块失败");
        }
    }


    private Boolean isAdmin(){
        return 2 == TokenUtils.getClaimByToken(httpServletRequest.getHeader("token"),"userid");
    }

}
